﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace SEO.Utility
{
    /// <summary>
    /// 版权所有: 版权所有(C) 2011，Adin Lee
    /// 内容摘要: 新浪微博API常量定义及其他处理
    /// 完成日期：2011年5月22日
    /// 版    本：V1.0 
    /// 作    者：Adin
    /// </summary>
    public class SinaOperate
    {
        #region Fields

        /// <summary>
        /// 不进行 UrlEncode 的字符
        /// </summary>
        private static string unreservedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~";

        /// <summary>
        /// 登录，身份验证，返回用户信息
        /// </summary>
        public static readonly string ACCOUNTVERIFY = "http://api.t.sina.com.cn/account/verify_credentials.xml";

        /// <summary>
        /// 读取我的关注的博客列表
        /// </summary>
        public static readonly string READFRIENDSBLOGS = "http://api.t.sina.com.cn/statuses/friends_timeline.json";

        /// <summary>
        /// 发表微博
        /// </summary>
        public static readonly string POSTBLOG = "http://api.t.sina.com.cn/statuses/update.json";

        /// <summary>
        /// 评论微博
        /// </summary>
        public static readonly string COMMENT = "http://api.t.sina.com.cn/statuses/comment.json";

        /// <summary>
        /// 转发微博
        /// </summary>
        public static readonly string REPOST = "http://api.t.sina.com.cn/statuses/repost.json";

        /// <summary>
        /// 收藏微博
        /// </summary>
        public static readonly string FAVORITED = "http://api.t.sina.com.cn/favorites/create.xml";

        /// <summary>
        /// 删除
        /// </summary>
        public static readonly string DESTROY = "http://api.t.sina.com.cn/statuses/destroy/:id.(json|xml)";

        /// <summary>
        /// 发布带图片的微博
        /// </summary>
        public static readonly string UPLOAD = "http://api.t.sina.com.cn/statuses/upload.xml";

        /// <summary>
        /// AppKey
        /// </summary>
        public static string APPKEY = System.Configuration.ConfigurationSettings.AppSettings["AppKey"];

        #endregion

        #region Methods


        /// <summary>
        /// 设置工作内存，降低内存占用
        /// </summary>
        /// <param name="maxWorkingSet">内存中允许的进程的最大工作集大小（以字节为单位）</param>
        public static void SetWorkingSet(int maxWorkingSet)
        {
            System.Diagnostics.Process.GetCurrentProcess().MaxWorkingSet = (IntPtr)maxWorkingSet;
        }

        /// <summary>
        /// 重写的 UrlEncode 函数，因为 HttpFunction.UrlEncode 转义的为小写在签名时可能遇到问题
        /// </summary>
        /// <param name="sourceString">要进行 UrlEncode 的源字符串</param>
        /// <returns>UrlEncode 转义后的字符串</returns>
        public static string UrlEncode(string sourceString)
        {
            if (string.IsNullOrEmpty(sourceString))
            {
                return "";
            }
            StringBuilder result = new StringBuilder();
            foreach (char symbol in sourceString)
            {
                if (unreservedChars.IndexOf(symbol) != -1)
                {
                    result.Append(symbol);
                }
                else
                {
                    byte[] temp = Encoding.UTF8.GetBytes(symbol.ToString());
                    foreach (byte tem in temp)
                    {
                        result.Append('%' + Convert.ToString(tem, 16).ToUpper());
                    }
                }
            }
            return result.ToString();
        }
        #endregion

        #region Enum
        /// <summary>
        /// 进行操作的类型
        /// </summary>
        public enum DealBlog
        {
            /// <summary>
            /// 发表
            /// </summary>
            POST,
            /// <summary>
            /// 评论
            /// </summary>
            COMMENT,
            /// <summary>
            /// 转发
            /// </summary>
            REPOST,
            /// <summary>
            /// 删除
            /// </summary>
            DELETE,
            /// <summary>
            /// 收藏
            /// </summary>
            FAVORITED,
            /// <summary>
            /// 无操作
            /// </summary>
            NONE
        }
        #endregion
    }
}
